package com.shaoshaohuo.app.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.MyBaseAdapter;
import com.shaoshaohuo.app.entity.CollectionEntity;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.ui.NewDetailsActivity;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.utils.ToastUtil;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/6/13.
 */

public class NewCollection extends BaseActivity implements View.OnClickListener {
    private Handler handler = new Handler();
    private ListView collec_list;
    private CheckBox collection_cx_all;
    private ImageView collection_iv_bj;
    private TextView collection_tv_shopsize;
    private String str = "全部商品(%d)";
    private Myadapter myadapter;
    private Button delte;
    private TextView textview_message;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_collection_layout);
        init();

        networkquery();
    }

    /**
     * 网络请求
     */
    private void networkquery() {
        ShopHttpConfig.querey(this, new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {

            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                CollectionEntity collectionEntity = (CollectionEntity) o;
                final List<CollectionEntity.DataBean> data = collectionEntity.getData();
                int size = data.size();
                final String format = String.format(str, size);

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        collection_tv_shopsize.setText(format);
                        myadapter.setData(data);

                    }
                });
            }
        }, CollectionEntity.class);
        collec_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Myadapter myadapter = (Myadapter) parent.getAdapter();
                List<CollectionEntity.DataBean> getdata = myadapter.getdata();
                if (getdata.get(position).getGoodStatus().equals("2")){
                    ToastUtil.toast("商品已过期");
                }
                else {
                    String catid = myadapter.getdata().get(position).getCatid();
                    Intent intent = new Intent(NewCollection.this, NewDetailsActivity.class);
                    intent.putExtra("catid", catid);
                    startActivity(intent);
                }
            }
        });
    }

    private void init() {
        collec_list = (ListView) findViewById(R.id.collec_list);
        List<CollectionEntity.DataBean> list = new ArrayList<>();
        myadapter = new Myadapter(this, list, true);
        collec_list.setAdapter(myadapter);
        collection_cx_all = (CheckBox) findViewById(R.id.collection_cx_all);
        collection_cx_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                List<CollectionEntity.DataBean> getdata = myadapter.getdata();
                for (int i = 0; i < getdata.size(); i++) {
                    CollectionEntity.DataBean dataBean = getdata.get(i);
                    dataBean.setIschecked(isChecked);
                }
                myadapter.notifyDataSetChanged();
            }
        });
        collection_iv_bj = (ImageView) findViewById(R.id.collection_iv_bj);

        collection_tv_shopsize = (TextView) findViewById(R.id.collection_tv_shopsize);
        collection_iv_bj.setOnClickListener(this);
        delte = (Button) findViewById(R.id.delte);
        delte.setOnClickListener(this);
        /**
         * topbarview
         */
        View viewById = findViewById(R.id.imageview_topbar_left_backImage);
        viewById.setVisibility(View.VISIBLE);
        textview_message = (TextView) findViewById(R.id.textview_message);
        textview_message.setOnClickListener(this);
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView toolbar_topname = (TextView) findViewById(R.id.toolbar_topname);
        toolbar_topname.setText("我的收藏");
        findViewById(R.id.imageview_message).setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.collection_iv_bj:
                collection_cx_all.setVisibility(View.VISIBLE);
                delte.setVisibility(View.VISIBLE);
                v.setVisibility(View.GONE);
                textview_message.setText("完成");
                textview_message.setVisibility(View.VISIBLE);
                List<CollectionEntity.DataBean> getdata = myadapter.getdata();
                for (int i = 0; i < getdata.size(); i++) {
                    CollectionEntity.DataBean dataBean = getdata.get(i);
                    dataBean.setIsvisb(true);
                }
                myadapter.notifyDataSetChanged();
                break;
            case R.id.textview_message:
                collection_cx_all.setVisibility(View.GONE);
                delte.setVisibility(View.GONE);
                collection_iv_bj.setVisibility(View.VISIBLE);
                textview_message.setVisibility(View.GONE);
                List<CollectionEntity.DataBean> getdata1 = myadapter.getdata();
                for (int i = 0; i < getdata1.size(); i++) {
                    CollectionEntity.DataBean dataBean = getdata1.get(i);
                    dataBean.setIsvisb(false);
                }
                myadapter.notifyDataSetChanged();
                break;
            case R.id.delte:
                String catids = "";
                List<CollectionEntity.DataBean> getdata2 = myadapter.getdata();
                for (int i = 0; i < getdata2.size(); i++) {
                    CollectionEntity.DataBean dataBean = getdata2.get(i);
                    boolean ischecked = dataBean.ischecked();
                    if (ischecked) {
                        Log.e("TAG", "onClick: ===============");
                        String catid = dataBean.getCatid();
                        if (catids.length() == 0) {
                            catids += catid;
                        } else {
                            catids += "_" + catid;
                        }
                    }
                }
                ShopHttpConfig.Collectiondelte(this, catids, new Okhttputils.OkutListener() {
                    @Override
                    public void error(Call call, IOException e) {

                    }

                    @Override
                    public void error(Call call, int code) {

                    }

                    @Override
                    public void suseff(Call call, Response response, Object o) {
                        List<CollectionEntity.DataBean> getdata3 = myadapter.getdata();
                        for (int i = getdata3.size() - 1; i >= 0; i--) {
                            CollectionEntity.DataBean dataBean = getdata3.get(i);
                            boolean ischecked = dataBean.ischecked();
                            if (ischecked) {
                                Log.e("TAG", "suseff: ");
                                getdata3.remove(dataBean);
                            }
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                myadapter.notifyDataSetChanged();
                                if (myadapter.getdata().size() == 0) {
                                    collection_cx_all.setVisibility(View.GONE);
                                    delte.setVisibility(View.GONE);
                                    collection_iv_bj.setVisibility(View.VISIBLE);
                                    textview_message.setVisibility(View.GONE);
                                    List<CollectionEntity.DataBean> getdata1 = myadapter.getdata();
                                    for (int i = 0; i < getdata1.size(); i++) {
                                        CollectionEntity.DataBean dataBean = getdata1.get(i);
                                        dataBean.setIsvisb(false);
                                    }
                                    myadapter.notifyDataSetChanged();
                                }
                            }
                        });
                    }
                });

                break;
        }
    }

    class Myadapter extends MyBaseAdapter<CollectionEntity.DataBean> implements CompoundButton.OnCheckedChangeListener {

        private final LayoutInflater from;

        public Myadapter(Context ctx, List<CollectionEntity.DataBean> list, boolean flag) {
            super(ctx, list, flag);
            from = LayoutInflater.from(ctx);
        }

        @Override
        public View getView(int pos, View view, ViewGroup group) {
            Myholder myholder;
            if (view == null) {
                view = from.inflate(R.layout.collection_listitem, null);
                myholder = new Myholder();
                myholder.tv_name = (TextView) view.findViewById(R.id.collectionitem_tvname);
                myholder.collectionitem_tv_mbn_details = (TextView) view.findViewById(R.id.collectionitem_tv_mbn_details);
                myholder.collectionitem_tv_details = (TextView) view.findViewById(R.id.collectionitem_tv_details);
                myholder.tv_shopmoney = (TextView) view.findViewById(R.id.tv_shopmoney);
                myholder.imageView2 = (ImageView) view.findViewById(R.id.imageView2);
                myholder.checkBox = (CheckBox) view.findViewById(R.id.collectionitem_cx);
                myholder.collectionitem_tv_level = (TextView) view.findViewById(R.id.collectionitem_tv_level);
                myholder.iv_guoqi = (ImageView) view.findViewById(R.id.iv_guoqi);
                view.setTag(myholder);
            } else {
                myholder = (Myholder) view.getTag();
            }

            CollectionEntity.DataBean dataBean = list.get(pos);
            boolean isvisb = dataBean.isvisb();
            if (isvisb) {
                myholder.checkBox.setVisibility(View.VISIBLE);
            } else {
                myholder.checkBox.setVisibility(View.GONE);
            }
            myholder.checkBox.setTag(pos);
            myholder.checkBox.setChecked(dataBean.ischecked());
            myholder.checkBox.setOnCheckedChangeListener(this);
            if (dataBean.getGoodStatus().equals("2")) {
                myholder.iv_guoqi.setVisibility(View.VISIBLE);
                myholder.iv_guoqi.setImageResource(R.mipmap.yiguoqi);
            } else {
                myholder.iv_guoqi.setVisibility(View.GONE);
            }

            String name = dataBean.getName();
            String details = dataBean.getDetails();
            String img = dataBean.getImg();
            String shop_money = dataBean.getShop_money();
            String mbn_details = dataBean.getMbn_details();
            myholder.tv_name.setText(name);
            myholder.collectionitem_tv_level.setText(dataBean.getLevel());
            myholder.collectionitem_tv_details.setText(details);
            if (dataBean.getMbn_detailsType().equals("1")) {
                myholder.tv_shopmoney.setText("￥" + shop_money + "/斤");
                myholder.collectionitem_tv_mbn_details.setText(mbn_details + "斤/件");
            }
            if (dataBean.getMbn_detailsType().equals("2")) {
                myholder.tv_shopmoney.setText("￥" + shop_money + "/个");
                myholder.collectionitem_tv_mbn_details.setText(mbn_details + "个/件");
            }

            Picasso.with(NewCollection.this).load(img).placeholder(R.mipmap.logo).placeholder(R.mipmap.zhanzhanzhan).into(myholder.imageView2);
            return super.getView(pos, view, group);
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int pos = (int) buttonView.getTag();
            list.get(pos).setIschecked(isChecked);
        }

        class Myholder {
            TextView tv_name, collectionitem_tv_mbn_details, collectionitem_tv_details, tv_shopmoney, collectionitem_tv_level;
            ImageView imageView2;
            CheckBox checkBox;
            ImageView iv_guoqi;
        }
    }
}
